package com.googlecode.dgwt.client.dojo.impl;

import java.util.Collection;

import com.googlecode.dgwt.client.dojo.string.RegExp;
import com.googlecode.dgwt.client.dojo.string.RegExpResult;
import com.googlecode.dgwt.client.dojo.util.JsUtil;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class HtmlImpl {

    
    protected RegExp XML_MARKUP = RegExp.create("^\\s*<\\?xml(\\s)+version=[\\'\\\"](\\d)*.(\\d)*[\\'\\\"](\\s)*\\?>", "im");
    
    protected RegExp HTML_MARKUP = RegExp.create("(?:\\s*<!DOCTYPE\\s[^>]+>|<title[^>]*>[\\s\\S]*?<\\/title>)", "ig");
    
    protected RegExp HTML_BODY = RegExp.create("<body[^>]*>\\s*([\\s\\S]+)\\s*<\\/body>", "im");
    
    protected RegExp CSS_REL_HREF = RegExp.create("\\s*(?:rel|href)=(['\"])?[^\\s]*\\1\\s*", "gi");

    protected RegExp CSS_LINK_PARAMS = RegExp.create("^\\s*([\\s\\S]*?)\\s*$", "i");

    
    public String sanitizeHTML(String html) {
        if (html != null) {
            html = html.trim();
            html = XML_MARKUP.replace(html, "");
            html = HTML_MARKUP.replace(html, "");
            RegExpResult body = HTML_BODY.exec(html);
            if (body != null) {
                html = body.get(1);
            }
            return html.trim();
        }
        return "";
    }
    
    
    public native String snarfStyles(/*String referencePath,*/ String html, Collection<String> styleSheets) /*-{
        var self = this;
        return (html||"").replace(/(?:<style([^>]*)>([\s\S]*?)<\/style>|<link\s+(?=[^>]*rel=['"]?stylesheet)([^>]*?href=(['"])([^>]*?)\4[^>\/]*)\/?>)/gi, 
            function(_22, _23, _24, _25, _26, _27){
                self.@com.googlecode.dgwt.client.dojo.impl.HtmlImpl::snarfStyles(Ljava/lang/String;Ljava/util/Collection;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(
                    null, styleSheets, _22, _23, _24, _25, _26, _27
                );
            }
        );
    }-*/;
    
    
    
    protected String snarfStyles(String referencePath, Collection<String> styleSheets, String _22, String _23, String _24, String _25, String _26, String _27) {
            //int i;
            String _29 = JsUtil.or(JsUtil.or(_23, _25), "");
            _29 = CSS_LINK_PARAMS.replace(_29, "$1");
            if (JsUtil.isNotEmpty(_24)) {
                styleSheets.add(/*JsUtil.notEmpty(referencePath) ? _adjustCssPaths(referencePath, _24) :*/ _24);
                //i = styleSheets.size();
            }
            else {
                styleSheets.add("@import \"" + _27 + "\";");
                //i = styleSheets.size();
                _29 = CSS_REL_HREF.replace(_29, "");
            }
            // FIXME referencePath
            /*
            if (_29) {
                String[] _29 = _29.split("\\s+");
                var _2a = {}, tmp;
                for (var j = 0, e = _29.length; j < e; j++) {
                    tmp = _29[j].split("=");
                    _2a[tmp[0]] = tmp[1].replace(/^\s*['"]?([\s\S]*?)['"]?\s*$/, "$1");
                }
                styleSheets.attributes[i - 1] = _2a;
            }
            */
            return "";
    }


    public String snarf(String html, Collection<String> styleSheets, Collection<String> scripts) {
        html = snarfStyles(html, styleSheets);
        //TODO: html = snarfScripts(html, scripts);
        return sanitizeHTML(html);
    }
    
}
